﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Giaiphongmatbang.DataModel;

namespace Giaiphongmatbang.BLL
{
    public class DistrictsRepository
    {
        private QLGPMBEntities data = new QLGPMBEntities();

        public IEnumerable<District> GetAll()
        {
            var results = from p in data.Districts
                          select p;
            return results;
        }

        public District GetByID(int ID)
        {
            var result = from p in data.Districts
                         where p.DistrictID == ID
                         select p;
            return result.FirstOrDefault();
        }

        public IEnumerable<District> GetByProvinceID(int ID)
        {
            var results = from p in data.Districts
                         where p.Province.ProvinceID == ID
                         select p;
            return results;
        }

        public void Insert(District d)
        {
            data.AddToDistricts(d);
        }

        public void DeleteByID(int id)
        {
            var obj = GetByID(id);
            if (obj != null)
            {
                VillagesRepostory v = new VillagesRepostory();
                if (v.GetByDistrictID(id).FirstOrDefault() == null)
                {
                    data.DeleteObject(obj);
                }
            }
        }

        public void Save()
        {
            data.SaveChanges();
        }
    }
}